//
//  ST_MemberSubscriberPaymentView.swift
//  btvideo
//
//  Created by mac on 2020/9/25.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import HandyJSON
import SnapKit
import SwifterSwift


//MARK: - Base Class
class ST_MemberSubscriberPaymentBaseView: UIControl {
    
    override var isSelected: Bool{
        didSet{
            modify_status(select: isSelected)
        }
    }
    
    var model = ST_JVPayItemViewModel(){
        didSet{
            
            top_label.text = model.topTitle
            
            cycle_title_label.text = model.limited_time.int.string
            
            let monthly = model.original_price / (model.limited_time.int == 0 ? 1 : model.limited_time.int).double
            
            monthly_price_title_label.text = String.init(format: "¥%.2f/月", monthly)
            
            thrift_title_label.text = model.thrift
            
            total_price_label.text =  String.init(format: "¥%d", model.original_price.int)
            
        }
    }
//    MARK: - Properties
    fileprivate lazy var top_label_normal_color = UIColor.init(hex: 0xACB4C0)
    
    fileprivate lazy var top_label_selected_color = UIColor.white
    
    fileprivate lazy var top_view:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xFEC207)
        control.alpha = 0
        return control
    }()
    
    fileprivate lazy var top_label:UILabel = {
        let control = UILabel.init()
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.medium)
        control.textColor = top_label_normal_color
        return control
    }()
    
    
    
    fileprivate lazy var cycle_title_label_normal_color = UIColor.init(hex: 0xACB4C0)
    
    fileprivate lazy var cycleTitleLabelSelectedColor = UIColor.init(hex: 0x333333)
    
    fileprivate lazy var cycle_title_label:UILabel = {
        let control = UILabel.init()
        control.textAlignment = .center
        control.font = UIFont.init(name: "DINPro-Black", size: 30)
        control.textColor = cycle_title_label_normal_color
        return control
    }()
    
    fileprivate lazy var cycle_subt_title_label:UILabel = {
        let control = UILabel.init()
        control.textAlignment = .center
        control.text = "个月"
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        control.textColor = cycle_title_label_normal_color
        return control
    }()
    

    fileprivate lazy var monthly_price_title_label:UILabel = {
        let control = UILabel.init()
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular)
        control.textColor = cycle_title_label_normal_color
        return control
    }()
    
    fileprivate lazy var thrift_title_label:UILabel = {
        let control = UILabel.init()
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium)
        control.textColor = UIColor.init(hex: 0xFEC207)
        return control
    }()
    
    
    fileprivate lazy var total_price_label_normal_color = UIColor.init(hex: 0x61666F)
    
    fileprivate lazy var total_price_label_selected_color = UIColor.init(hex: 0x191919)
    
    fileprivate lazy var total_price_label:UILabel = {
        let control = UILabel.init()
        control.textAlignment = .center
        control.font = UIFont.init(name: "DINPro-Black", size: 18)
        control.textColor = total_price_label_normal_color
        return control
    }()
    
    
    
    func modify_status(select:Bool){
    }
}


//MARK: - Special Class

class ST_MemberSubscriberPaymentView: ST_MemberSubscriberPaymentBaseView {

    override init(frame: CGRect) {
        super.init(frame: frame)
       
        self.cornerRadius = 15
        
        self.addSubview(top_view)
        top_view.snp.makeConstraints { (make) in
            make.top.right.left.equalToSuperview()
            make.height.equalTo(25)
        }
        
        self.addSubview(top_label)
        top_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(19)
            make.height.equalTo(17)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(cycle_title_label)
        cycle_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(40)
            make.height.equalTo(38)
            make.centerX.equalToSuperview()
        }
        
        
        self.addSubview(cycle_subt_title_label)
        cycle_subt_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(79)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(monthly_price_title_label)
        monthly_price_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(99)
            make.height.equalTo(17)
            make.centerX.equalToSuperview()
        }
        
        
        self.addSubview(thrift_title_label)
        thrift_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(121)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
        }
        self.addSubview(total_price_label)
        total_price_label.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-8)
            make.height.equalTo(29)
            make.centerX.equalToSuperview()
        }
        
//        top_label.transform
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK: - layout for status
    override func modify_status(select:Bool){
        
        if select{
            self.backgroundColor = UIColor.init(hex: 0xFFFBEC)
            self.borderColor = UIColor.init(hex: 0xFEC207)
            self.borderWidth = 1
            
            UIView.animate(withDuration: 0.2) {
                
                self.top_view.alpha = 1
                self.top_label.transform = CGAffineTransform(translationX: 0, y: -19+4)
                self.top_label.textColor = self.top_label_selected_color
                
                self.cycle_title_label.transform = CGAffineTransform.transform_from_rect(from: CGRect.init(origin: CGPoint.init(x: 0, y: 40), size: CGSize.init(width: 1, height: 38)), toRect: CGRect.init(origin: CGPoint.init(x: 0, y: 25), size: CGSize.init(width: 1.34, height: 51)))
                    
                self.cycle_title_label.textColor = self.cycleTitleLabelSelectedColor
                
                self.cycle_subt_title_label.transform = CGAffineTransform(translationX: 0, y: -2)
                    
                self.cycle_subt_title_label.textColor = self.cycleTitleLabelSelectedColor
                
                self.monthly_price_title_label.transform = CGAffineTransform(translationX: 0, y: -2)
                
                
                self.thrift_title_label.transform = CGAffineTransform.transform_from_rect(from: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 1, height: 20)), toRect: CGRect.init(origin: CGPoint.init(x: 0, y: -2), size: CGSize.init(width: 1.1, height: 22)))
                
                
                self.total_price_label.transform = CGAffineTransform.transform_from_rect(from: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 1, height: 29)), toRect: CGRect.init(origin: CGPoint.init(x: 0, y: -3), size: CGSize.init(width: 1.1, height: 32)))
                    
                self.total_price_label.textColor = self.total_price_label_selected_color
                
            }
        }
        else{
        
            self.backgroundColor = UIColor.clear
            self.borderWidth = 0
            
            UIView.animate(withDuration: 0.2) {
                self.top_view.alpha = 0
                
                self.top_label.transform = CGAffineTransform.identity
                
                self.top_label.textColor = self.top_label_normal_color
                
                self.cycle_title_label.transform = CGAffineTransform.identity
                
                self.cycle_title_label.textColor = self.cycle_title_label_normal_color
                
                self.cycle_subt_title_label.transform = CGAffineTransform.identity
                
                self.cycle_subt_title_label.textColor = self.cycle_title_label_normal_color
                
                self.monthly_price_title_label.transform = CGAffineTransform.identity
                
                self.thrift_title_label.transform = CGAffineTransform.identity
                
                self.total_price_label.transform = CGAffineTransform.identity
                
                self.total_price_label.textColor = self.total_price_label_normal_color
            }
            
        }
        
    }
}







//MARK: - Normal Class
class ST_MemberSubscriberBasicsPaymentView: ST_MemberSubscriberPaymentBaseView {

    override init(frame: CGRect) {
        super.init(frame: frame)
       
        self.cornerRadius = 15
        
        
        self.addSubview(cycle_title_label)
        cycle_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(52)
            make.height.equalTo(38)
            make.centerX.equalToSuperview()
        }
        
        
        self.addSubview(cycle_subt_title_label)
        cycle_subt_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(91)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        self.addSubview(monthly_price_title_label)
        monthly_price_title_label.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(111)
            make.height.equalTo(17)
            make.centerX.equalToSuperview()
        }
        
        
        self.addSubview(total_price_label)
        total_price_label.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-8)
            make.height.equalTo(29)
            make.centerX.equalToSuperview()
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    
//    MARK: - layout for status
    override func modify_status(select:Bool){
        
        if select{
            self.backgroundColor = UIColor.init(hex: 0xFFFBEC)
            self.borderColor = UIColor.init(hex: 0xFEC207)
            self.borderWidth = 1
            
            UIView.animate(withDuration: 0.2) {
                
                
                self.cycle_title_label.transform = CGAffineTransform.transform_from_rect(from: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 1, height: 30)), toRect: CGRect.init(origin: CGPoint.init(x: 0, y: -13), size: CGSize.init(width: 1.34, height: 40)))
                    
                self.cycle_title_label.textColor = self.cycleTitleLabelSelectedColor
                
                self.cycle_subt_title_label.transform = CGAffineTransform(translationX: 0, y: -2)
                    
                self.cycle_subt_title_label.textColor = self.cycleTitleLabelSelectedColor
                
                self.monthly_price_title_label.transform = CGAffineTransform(translationX: 0, y: -2)
                
                
                
                self.total_price_label.transform = CGAffineTransform.transform_from_rect(from: CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: CGSize.init(width: 1, height: 29)), toRect: CGRect.init(origin: CGPoint.init(x: 0, y: -3), size: CGSize.init(width: 1.1, height: 32)))
                    
                self.total_price_label.textColor = self.total_price_label_selected_color
                
            }
        }
        else{
        
            self.backgroundColor = UIColor.clear
            self.borderWidth = 0
            
            UIView.animate(withDuration: 0.2) {
                
                self.cycle_title_label.transform = CGAffineTransform.identity
                
                self.cycle_title_label.textColor = self.cycle_title_label_normal_color
                
                self.cycle_subt_title_label.transform = CGAffineTransform.identity
                
                self.cycle_subt_title_label.textColor = self.cycle_title_label_normal_color
                
                self.monthly_price_title_label.transform = CGAffineTransform.identity
                
                self.total_price_label.transform = CGAffineTransform.identity
                
                self.total_price_label.textColor = self.total_price_label_normal_color
            }
            
        }
        
    }
    

}
